vars <- c('any', 'high', 'spike')

roundfun <- function(x) {
  return(format(round(x, digits = 3), format='f', digits=3))
}
source(paste("config_code/indo_data_setup.R",sep = ""))
T = 2014-2004
means <- array("",
               dim=c(T,3),
               dimnames=list(2005:2014,
                             vars)
)

means['2005','any'] <- roundfun(mean(dta[dta$adm_year==2006, 
                                      "rhs_snpk_incidentNOCRIMEdumL1"]))
means['2005','high'] <- roundfun(mean(dta[dta$adm_year==2006, 
                                       "rhs_snpk_highincNOCRIMEdumL1"]))
for (v in vars) {
  source(paste("config_code/indo_dependent_vars.R",sep = ""))
  means['2006',v] <- roundfun(mean(dta[dta$adm_year==2006, 
                                          lagdv]))
  for (y in 2006:2013) {
    means[toString(y+1),v] <- roundfun(mean(dta[dta$adm_year==y, 
                                       dv]))
    
  }
}

head.height = '2'
head.width = '2'

out <- "\\begin{tabular}{lccc} "
out <- c(out,
         "\\hline \\hline",
         paste0(' & \\begin{minipage}[t][',
                head.height,
                'cm][b]{',
                head.width,
                'cm}\\begin{center} ',
                paste(c("Any Conflict",
                        "$\\geq 5$ Conflict Incidents",
                        "$\\geq 1$ std. dev. increase in Incidents"), 
                      collapse=paste0('\\end{center}\\end{minipage} & \\begin{minipage}[t][',
                                     head.height,
                                     'cm][b]{',
                                     head.width,
                                     'cm}\\begin{center} ')
                      ),
                '\\end{center}\\end{minipage} \\\\'),
         " & (1) & (2) & (3) \\\\ \\cmidrule{2-4} \\\\"
)

for (y in 2005:2014) {
  line <- paste0(y,
                 " & ",
                 paste(means[toString(roundfun(y)),],
                       collapse=" & "),
                 " \\\\ ")
  out <- c(out,
           line)
}
out <- c(out,
         "\\hline\\hline",
         "\\end{tabular}")

write(out,file='tables/tableC2.tex')


###############
# COLOMBIA
###############


source(paste("config_code/colo_data_setup.R",sep = ""))
T = 2005-1991
means <- array("",
               dim=c(T,3),
               dimnames=list(1992:2005,
                             vars)
)

for (v in vars) {
  source(paste("config_code/colo_dependent_vars.R",sep = ""))
  for (y in 1991:2004) {
    means[toString(y+1),v] <- roundfun(mean(dta[dta$year==y, 
                                             dv]))
    
  }
}

head.height = '2'
head.width = '2'

out <- "\\begin{tabular}{lccc} "
out <- c(out,
         "\\hline \\hline",
         paste0(' & \\begin{minipage}[t][',
                head.height,
                'cm][b]{',
                head.width,
                'cm}\\begin{center} ',
                paste(c("Any Conflict",
                        "$\\geq 5$ Conflict Incidents",
                        "$\\geq 1$ std. dev. increase in Incidents"), 
                      collapse=paste0('\\end{center}\\end{minipage} & \\begin{minipage}[t][',
                                      head.height,
                                      'cm][b]{',
                                      head.width,
                                      'cm}\\begin{center} ')
                ),
                '\\end{center}\\end{minipage} \\\\'),
         " & (1) & (2) & (3) \\\\ \\cmidrule{2-4} \\\\"
)

for (y in 1992:2005) {
  line <- paste0(y,
                 " & ",
                 paste(means[toString(roundfun(y)),],
                       collapse=" & "),
                 " \\\\ ")
  out <- c(out,
           line)
}
out <- c(out,
         "\\hline\\hline",
         "\\end{tabular}")

write(out,file='tables/tableC3.tex')
 